package haida.pym_system.site.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import haida.pym_system.site.pojo.User;
import haida.pym_system.site.pojo.Vo;
import haida.pym_system.site.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 大龙
 * @since 2020-09-28
 */
@Api(tags = "用户操作")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @ApiOperation("登录用户")
    @PostMapping
    @RequiresGuest
    public ResponseEntity<Object> userLogin(@RequestBody User loginForm) {
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginForm.getUsername(), loginForm.getPassword());

        System.out.println(loginForm.getUsername() + "===========" + loginForm.getPassword());

        try {
            subject.login(token); //没有异常就说明登录成功
        } catch (UnknownAccountException e) {
            return new ResponseEntity<>("账号错误！！！", HttpStatus.NO_CONTENT);
        } catch (IncorrectCredentialsException e) {
            return new ResponseEntity<>("密码错误！！！", HttpStatus.NO_CONTENT);
        }

        return new ResponseEntity<>("200", HttpStatus.OK);

    }

    @ApiOperation("注册用户")
    @PostMapping("/addUser")
    @RequiresGuest
    public ResponseEntity<Object> userRegister(@RequestBody User user) {
        System.out.println(user);
        userService.registerUser(user);
        return new ResponseEntity<>("注册成功！！！", HttpStatus.OK);
    }

    @ApiOperation("退出用户")
    @DeleteMapping
    @RequiresAuthentication
    public ResponseEntity<Object> userOut() {
        System.out.println("退出用户");
        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        return new ResponseEntity<>("退出用户成功！", HttpStatus.OK);
    }

    @ApiOperation("更新用户")
    @PutMapping
    public ResponseEntity<Object> userUpdate(@RequestBody User user) {
        System.out.println("更新用户");
        userService.updateUser(user);
        return new ResponseEntity<>("修改密码成功！！！", HttpStatus.OK);
    }

    @ApiOperation("获取全部用户")
    @GetMapping(path = "/allUser")
    public ResponseEntity<Object> getAllUsers() {
        System.out.println("获取全部用户");
        List<User> allUser = userService.getAllUser();
        return new ResponseEntity<>(allUser, HttpStatus.OK);
    }

    @ApiOperation("根据用户名字查询")
    @GetMapping(path = "/findUserByName")
    public ResponseEntity<Object> findUserName(String username) {
        System.out.println("根据用户名字查询");
        User user = userService.findUserName(username);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @ApiOperation("根据用户ID查询")
    @GetMapping(path = "/findUserByID")
    public ResponseEntity<Object> findUserName(int id) {
        System.out.println("根据用户名字查询");
        System.out.println(id);
        User user = userService.findUserByID(id);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @ApiOperation("分页查询用户")
    @GetMapping(path = "/queryUserForPage")
    public ResponseEntity<Object> queryUserForPage(String content, int current, int size) {
        System.out.println("分页查询用户");
        Vo<User> userVo = userService.queryUserForPage(content, current, size);
        System.out.println(userVo);

        return new ResponseEntity<>(userVo, HttpStatus.OK);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUser/{id}")
    public ResponseEntity<Object> deleteUser(@PathVariable int id) {
        System.out.println("删除用户");
        userService.deleteUser(id);
        return new ResponseEntity<>("删除用户成功！", HttpStatus.OK);
    }

    @ApiOperation("设置用户状态")
    @PutMapping("/setUserState/{userID}/{setV}")
    public ResponseEntity<Object> setUserState(@PathVariable int userID,@PathVariable int setV) {
        System.out.println("设置用户状态");
        userService.setUserState(userID,setV);
        return new ResponseEntity<>("设置用户成功！", HttpStatus.OK);
    }
}
